/**
 * @(#)Main.java 2011-8-24 Copyright 2011 it.kedacom.com, Inc. All rights
 *               reserved.
 */

package com.kedacom.xmpp.demo.crossserver;

import java.util.List;

import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.XMPPError;
import org.jivesoftware.smackx.pubsub.AccessModel;
import org.jivesoftware.smackx.pubsub.ConfigureForm;
import org.jivesoftware.smackx.pubsub.FormType;
import org.jivesoftware.smackx.pubsub.ItemPublishEvent;
import org.jivesoftware.smackx.pubsub.LeafNode;
import org.jivesoftware.smackx.pubsub.PayloadItem;
import org.jivesoftware.smackx.pubsub.PubSubManager;
import org.jivesoftware.smackx.pubsub.PublishModel;
import org.jivesoftware.smackx.pubsub.SimplePayload;
import org.jivesoftware.smackx.pubsub.listener.ItemEventListener;

/**
 * (用一句话描述类的主要功能)
 * @author xiaojinghai
 * @date 2011-8-24
 */

public class PubSubBetweenServers {

	/**
	 * (用一句话描述方法的主要功能)
	 * @param args
	 * @throws XMPPException
	 */

	public static void main(String[] args) throws XMPPException {
		Connection.DEBUG_ENABLED = true;
		String SERVER1 = "kedacom.local", SERVER2 = "163.local", RESOURCE = "java", USER1 = "admin", PASS1 = "itserver", USER2 = "admin", PASS2 = "itserver";
		String TOPIC = "myTopic";
		// String TOPIC = "myTopic@" + SERVER1;
		Connection conn1 = null, conn2 = null;
		// 连接与登录
		conn1 = new XMPPConnection(SERVER1);
		conn1.connect();
		conn1.login(USER1, PASS1, RESOURCE);

		// 获得pub/sub管理器
		PubSubManager mgr = new PubSubManager(conn1);
		// 创建一个带配置的结点/主题
		LeafNode node = null;
		mgr.deleteNode(TOPIC);
		try {
			node = (LeafNode) mgr.getNode(TOPIC);
			// node.sendConfigurationForm(form);
			// node = (LeafNode) mgr.createNode(TOPIC, form);
		} catch (XMPPException e) {
			XMPPError xe = e.getXMPPError();
			if (xe.getCode() == 404) {// 结点不存在
				// 创建一个带配置的结点/主题
				ConfigureForm form = new ConfigureForm(FormType.submit);
				form.setAccessModel(AccessModel.open);
				form.setDeliverPayloads(true);
				form.setNotifyRetract(true);
				form.setPersistentItems(true);
				form.setPublishModel(PublishModel.open);
				form.setMaxItems(3);
				node = (LeafNode) mgr.createNode(TOPIC, form);
			}
			if (xe.getCode() == 409) {// 结点已存在
				mgr.deleteNode(TOPIC);
			}
		}

		conn2 = new XMPPConnection(SERVER2);
		conn2.connect();
		conn2.login(USER2, PASS2, RESOURCE);
		PubSubManager mgr2 = new PubSubManager(conn2, "pubsub.kedacom.local");
		// PubSubManager mgr2 = new PubSubManager(conn2);
		LeafNode node2 = (LeafNode) mgr2.getNode(TOPIC);
		// 接收信息
		node2.addItemEventListener(new ItemEventListener<PayloadItem<SimplePayload>>() {

			public void handlePublishedItems(ItemPublishEvent<PayloadItem<SimplePayload>> event) {
				System.out.println("收到消息:");
				List<PayloadItem<SimplePayload>> items = event.getItems();
				for (PayloadItem<SimplePayload> item : items) {
					SimplePayload payload = item.getPayload();
					System.out.println("payload xml:" + payload.toXML());
				}
			}
		});

		// node2.subscribe(USER2 + "@" + SERVER2);
		node2.subscribe(USER2 + "@" + SERVER2 + "/" + RESOURCE);
		// 发布信息
		node.send(new PayloadItem<SimplePayload>(new SimplePayload("book", "pubsub:test:book",
				"<book xmlns='pubsub:test:book'><name>书名2</name></book>")));
		node2.send(new PayloadItem<SimplePayload>(new SimplePayload("book", "http://jabber.org/protocol/pubsub",
				"<book xmlns='http://jabber.org/protocol/pubsub'><name>书名5</name></book>")));

		Thread.currentThread().suspend();
	}
}
